package main

import (
	"db_demo/db"
	"fmt"
	"os"
	"strings"
)

func main() {
	//dbTest()
	fileRead()
}

func fileRead() {

	data, err := os.ReadFile("11.txt")
	if err != nil {
		return
	}
	dataLines := strings.Split(string(data), "\r\n")
	//打开链接
	db.Open("mysql", "fuly", "fuly@cnns", "10.10.27.177:3306", "ascription")

	query := "INSERT INTO ascription_bankcard(bank_id,bank_name,card_length,bin_length,bin,card_type,card_name) VALUES (?,?,?,?,?,?,?) ON DUPLICATE KEY UPDATE update_code=1"

	for index, line := range dataLines {
		fmt.Printf("行下标：%d\n", index)
		str := strings.Fields(line)
		if len(str) == 0 {
			break
		}
		r, e := db.Db.Exec(query, str[0], str[1], str[2], str[3], str[4], str[5], str[6])
		if e != nil {
			fmt.Println(e)
			continue
		}
		fmt.Println(r)
	}

	db.Db.Close()

}

func dbTest() {
	driverName := "mysql"
	userName := "fuly"
	password := "fuly@cnns"
	host := "10.10.27.177:3306"
	//查询数据库列表
	loadSchemaList := db.LoadSchemaList(driverName, userName, password, host)
	//遍历数据库名称列表
	for _, schemaName := range loadSchemaList {
		//schemaName := loadSchemaList[index]
		fmt.Printf("加载的数据库：%s\n", schemaName)
		//加载数据库下的表格
		loadTableList := db.LoadTableList(driverName, userName, password, host, schemaName)
		//遍历数据库下的全部表格
		for _, tableName := range loadTableList {
			//tableName := loadTableList[tableIndex]
			fmt.Println(fmt.Sprintf("数据库名称：%s 表格名称：%s", schemaName, tableName))
			//查询列字段
			loadColumnList := db.LoadColumnList(driverName, userName, password, host, schemaName, tableName)
			for index := 0; index < len(loadColumnList); index++ {
				a := loadColumnList[index]
				fmt.Printf("ColumnName:%s, DataType:%s, ColumnLen:%d\n", a.ColumnName, a.DataType, a.ColumnLen)
			}
		}
	}
}
